/*
 *  Confidential and Proprietary                                                                
 Copyright 2008 By                                                                                     
 SGAI & Hewlett-Packard Development Company, L.P. 	              
 All Rights Reserved                                                                                  

 Project Name : SGAI  MES                                                                                                                                       
 Class Name   : AbstractL4Message.java    
 Package      : com.hp.message.vo      
 */
package com.erdos.message.vo;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

/**
 * this is designed to define a parent message for others to extend.
 @version     $Id: AbstractL4Message.java 9110 2012-03-08 02:08:48Z hubin $                                                          
 @author sunf
 @since  2008-9-4 
 */
public abstract class AbstractL4Message implements L4Messaging, XmlNameSpaceSupport {
	private static final long serialVersionUID = -6904826628397864925L;
	private static final String messageProvider=L4Messaging.class.getSimpleName();
	
	private static final String xmlVersionDeclaration="<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	/*
	 * default prefix for all L4 message
	 */
	private static final String PREFIX="ns1";	
	/*
	 * default name space front part for L4 message
	 */
	private static final String NAMESPACE="xmlns";
	/**
	 * message header which holds the non-body information
	 */
	private L4MessageHeader messageHeader;	
	
	@XStreamOmitField
	private String messageId;
	
	public AbstractL4Message() {
		this.messageHeader=new L4MessageHeader();
		this.messageHeader.setMessageTypeId(this.getMessageTypeId());
	}
	
	/**
	 * @return the messageHeader
	 */
	public L4MessageHeader getMessageHeader() {
		return messageHeader;
	}

	/**
	 * @param messageHeader the messageHeader to set
	 * do not call this method, it's designed for generating the xml.
	 */
	public void setMessageHeader(L4MessageHeader messageHeader) {
		this.messageHeader = messageHeader;
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L4Messaging#getCorrelationMessageId()
	 */
	public String getCorrelationMessageId() {
		return this.messageHeader.getCorrelationMessageId();
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L4Messaging#getCorrelationMessageTypeId()
	 */
	public String getCorrelationMessageTypeId() {
		return this.messageHeader.getCorrelationMessageTypeId();
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L4Messaging#getMessageId()
	 */
	public String getMessageId() {
		return this.messageId;
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L4Messaging#getReceiver()
	 */
	public String getReceiver() {
		return this.messageHeader.getReceiver();
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L4Messaging#getSendDateTime()
	 */
	public Date getSendDateTime() {
		return this.messageHeader.getSendDateTime();
	}

	/* (non-Javadoc)
	 * @see com.hp.message.vo.L4Messaging#getSender()
	 */
	public String getSender() {
		return this.messageHeader.getSender();
	}
	
	public Integer getLength() {		
		return this.messageHeader.getLength();
	}

	/**
	 * default name space string.
	 */
	public final String getNameSpace() {
		String nsUri=this.getNameSpaceUri();
		StringBuilder ns=new StringBuilder("");
		if(StringUtils.isNotEmpty(nsUri)) {			
			String prefix=this.getPrefix();
			if(StringUtils.isNotBlank(prefix)) {
				ns.append(NAMESPACE).append(":").append(prefix).append("=\"").append(nsUri).append("\"");				
			} else {
				ns.append(NAMESPACE).append(prefix).append("=\"").append(nsUri).append("\"");
			}
		}

		return ns.toString();
	}
	
	public void setMessageId(String msgId) {
		this.messageId = msgId;
	}
	public String getMessageProvider() {
		return messageProvider;
	}
	/**
	 * default prefix string.
	 */
	public String getPrefix() {
		return PREFIX;
	}
	
	public String getXmlVersionDeclaration() {
		return xmlVersionDeclaration;
	}

	/**
	 * for subclasses to extend to supply the name space URI.
	 * @return URI of the name space
	 * @author sunf
	 * @since  2008-9-10
	 */
	protected String getNameSpaceUri() {
		return "";
	}

	public String toString() {
		return new ToStringBuilder(this).append(this.messageHeader).toString();
	}
}
